package com.graduate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduate.common.R;
import com.graduate.dto.GoodsDto;
import com.graduate.dto.SetMealDto;
import com.graduate.entity.Category;
import com.graduate.entity.Goods;
import com.graduate.entity.SetMeal;
import com.graduate.entity.SetmealGoods;
import com.graduate.service.CategoryService;
import com.graduate.service.GoodsService;
import com.graduate.service.SetMealService;
import com.graduate.service.SetmealGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

//套餐管理
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private SetmealGoodsService setmealGoodsService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetMealDto setMealDto) {
        log.info("套餐信息：{}", setMealDto);
        setMealService.saveWithGoods(setMealDto);
        return R.success("新增套餐成功");
    }

    //套餐分页查询
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器对象
        Page<SetMeal> pageInfo = new Page<>(page, pageSize);
        Page<SetMealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name != null, SetMeal::getName, name);
        //添加排序条件，根据更新时间降序排
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        setMealService.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<SetMeal> records = pageInfo.getRecords();

        List<SetMealDto> list = records.stream().map((item) -> {
            SetMealDto setMealDto = new SetMealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setMealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setMealDto.setCategoryName(categoryName);
            }
            return setMealDto;

        }).collect(Collectors.toList());

        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }


    //删除套餐，批量删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);

        setMealService.removeWithGoods(ids);
        return R.success("套餐数据删除成功");
    }

    //起售停售套餐，批量起售停售
    @PostMapping("/status/{status}")
    public R<String> updateMulStatus(@PathVariable Integer status, Long[] ids) {
        List<Long> list = Arrays.asList(ids);

        //构造条件构造器
        LambdaUpdateWrapper<SetMeal> updateWrapper = new LambdaUpdateWrapper<>();
        //添加过滤条件
        updateWrapper.set(SetMeal::getStatus, status).in(SetMeal::getId, list);
        setMealService.update(updateWrapper);

        return R.success("套餐信息修改成功");
    }

    //根据id查询套餐信息和对应的商品信息
    @GetMapping("/{id}")
    public R<SetMealDto> get(@PathVariable Long id) {

        SetMealDto setmealDto = setMealService.getByIdWithGoods(id);
        return R.success(setmealDto);
    }


    // 更新套餐信息，同时更新对应的商品信息
    @PutMapping
    public R<String> update(@RequestBody SetMealDto setmealDto) {
        log.info(setmealDto.toString());

        setMealService.updateWithGoods(setmealDto);

        return R.success("修改商品成功");
    }

    //根据条件查询套餐信息
    @GetMapping("/list")
    public R<List<SetMeal>> list(SetMeal setMeal) {
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setMeal.getCategoryId() != null, SetMeal::getCategoryId, setMeal.getCategoryId());
        queryWrapper.eq(setMeal.getStatus() != null, SetMeal::getStatus, setMeal.getStatus());
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);
        List<SetMeal> list = setMealService.list(queryWrapper);
        return R.success(list);
    }

    //移动端点击套餐图片查看套餐具体内容
    @GetMapping("/goods/{id}")
    public R<List<GoodsDto>> goods(@PathVariable("id") Long SetmealId) {
        LambdaQueryWrapper<SetmealGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealGoods::getSetmealId, SetmealId);
        //获取套餐里面的所有商品
        List<SetmealGoods> list = setmealGoodsService.list(queryWrapper);

        List<GoodsDto> dishDtos = list.stream().map((setmealDish) -> {
            GoodsDto goodsDto = new GoodsDto();
            //其实这个BeanUtils的拷贝是浅拷贝
            BeanUtils.copyProperties(setmealDish, goodsDto);
            //这里是为了把套餐中的菜品的基本信息填充到dto中，比如菜品描述，菜品图片等菜品的基本信息
            Long goodsId = setmealDish.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            BeanUtils.copyProperties(goods, goodsDto);

            return goodsDto;
        }).collect(Collectors.toList());

        return R.success(dishDtos);
    }
}
